/**
 * 
 */
package org.bankabeule.ui.wizards;

import org.bankabeule.Tag;
import org.bankabeule.core.ModelManager;
import org.bankabeule.ui.utils.Logger;
import org.eclipse.jface.wizard.Wizard;

/**
 * @author sdudoit
 *
 */
public class TagWizard extends Wizard {

	
	/**
	 * Tag
	 */
	private Tag tag;
	
	/**
	 * Parent tag
	 */
	private Tag parentTag;
	
	/**
	 * Tag page
	 */
	private TagPage page;
	
	
	/**
	 * Constructor
	 */
	public TagWizard(Tag existingTag, Tag parentTag) {
		this.tag = existingTag;
		this.parentTag = parentTag;
		setWindowTitle("Edit tag");
	}
	
	
    /**
     * Gets the federate.
     */
    public void addPages() {
    	page = new TagPage("TagPage", tag, parentTag);
        addPage(page);
    }
    
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
	@Override
	public boolean performFinish() {
		try {
			if (tag == null) {
				// new tag to add
				tag = page.getTag();
				if (parentTag == null) {
					ModelManager.get().getModel().getTags().add(tag);					
				} else {
					parentTag.getTags().add(tag);
				}
			} else {
				// update tag
				tag.setName(page.getTag().getName());
				tag.setDescription(page.getTag().getDescription());
			}
			return true;
		} catch (Exception e) {
			Logger.log(e);
			return false;
		}
	}

}
